package mx.ipn.persistencia.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import mx.ipn.persistencia.dao.StatusDAO;
import mx.ipn.to.StatusTO;

public class StatusDAOMySQL implements StatusDAO {
	static{FabricaDeDAOsMySQL.iniciaLog(StatusDAOMySQL.class);}

	private static final String INSERT = "INSERT INTO status (nombre) VALUES(?)";
	private static final String DELETE = "DELETE FROM status WHERE id_status=?";
	private static final String UPDATE = "UPDATE status SET nombre=? WHERE id_status=?";
	private static final String FINDBYID = "SELECT * FROM status WHERE id_status=?";
	private static final String FINDBYNOM = "SELECT * FROM status WHERE nombre=?";
	private static final String SELECT = "SELECT * FROM status";

	@Override
	public short deleteStatus(short idStatus) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(DELETE);

			ps.setShort(1, idStatus);

			ps.executeUpdate();
			if (ps.getUpdateCount() == 0) {
				ps.close();
				conexion.close();
				return 0;
			} else {
				ps.close();
				conexion.close();
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (short)-1;
		}
	}

	@Override
	public StatusTO findStatusById(short idStatus) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(FINDBYID);

			ps.setShort(1, idStatus);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				String nombre = rs.getString("nombre");

				StatusTO statusTO = new StatusTO(idStatus, nombre);
				ps.close();
				conexion.close();
				return statusTO;
			}
			ps.close();
			conexion.close();
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public StatusTO findStatusByNombre(String nombre) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(FINDBYNOM);

			ps.setString(1, nombre);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				short idStatus = rs.getShort("id_status");

				StatusTO statusTO = new StatusTO(idStatus, nombre);
				ps.close();
				conexion.close();
				return statusTO;
			}
			ps.close();
			conexion.close();
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean insertStatus(StatusTO statusTO) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(INSERT);

			ps.setString(1, statusTO.getNombre());
			ps.executeUpdate();

			ps.close();
			conexion.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public ArrayList<StatusTO> selectStatus() {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(SELECT);

			ResultSet rs = ps.executeQuery();

			ArrayList<StatusTO> coleccion = null;

			while (rs.next()) {
				if (coleccion == null)
					coleccion = new ArrayList<StatusTO>();

				short idStatus = rs.getShort("id_status");
				String nombre = rs.getString("nombre");

				StatusTO statusTO = new StatusTO(idStatus, nombre);
				coleccion.add(statusTO);
			}
			ps.close();
			conexion.close();

			return coleccion;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public short updateStatus(StatusTO statusTO) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(UPDATE);

			ps.setString(1, statusTO.getNombre());
			ps.setShort(2, statusTO.getIdStatus());

			ps.executeUpdate();

			if (ps.getUpdateCount() == 0) {
				ps.close();
				conexion.close();
				return 0;
			} else {
				ps.close();
				conexion.close();
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (short)-1;
		}
	}

}
